 
#include <bits/stdc++.h>

struct ErrorMsg{
    ErrorMsg(std::string && Msg,int code)
    :_MsgString(Msg)
    ,_ErorrCode(code)
    {

    }
    std::string _MsgString;
    int _ErorrCode;
};

enum 
{
    Noerror = 0,
    Div_zero = 1,
    Mod_zero = 2,
    Unkown = 3
};
//本类要求转换符合要求
class Calculate{
    
    std::string _exp;
    std::stack<char> _op;
    std::stack<std::string> _num;
    const std::string Illegal_Op = "+-*/%^()";
   int operator_Priority(char op) {  
    switch (op) {  
        case '^':  
            return 3; // 求幂优先级最高  
        case '*':  
        case '/':  
        case '%':  
            return 2; // 乘除取模优先级次之  
        case '+':  
        case '-':  
            return 1; // 加减优先级最低  
        case '(':  
            return -1;
        case ')':  
            return -1; //反括号表示立即就需要处理，向前运算
        default:  
            throw ErrorMsg("Invalid operator provided.",Unkown);  
    }  
    }
    public:
    double operator()(const std::string &s)
    {

        std::string x1;
        std::string x2;
        int numBegin = 0;
        for (int i = 0; i < s.size();)
        {
            switch (s[i]) {  
                case ')':  
                    {
                        while(!_op.empty() &&  _op.top() != '(')
                        {
                            char l_op = _op.top();
                            _op.pop();
                            x1 = _num.top();
                            _num.pop();
                            x2 = _num.top();
                            _num.pop();
                            _num.push(std::to_string(Cal(x2,x1,l_op)));
                        }
                        _op.pop();
                        numBegin = ++i;
                        break;
                    }
                case '^':  
                case '*':  
                case '/':  
                case '%':  
                case '+':  
                case '-':  
                case '(':  
                        if(s[i]=='(')
                        {

                        }
                        else
                        {
                            while(!_op.empty()&& operator_Priority(s[i])<operator_Priority(_op.top()))
                            {
                                x1 = _num.top();
                                _num.pop();
                                x2 = _num.top();
                                _num.pop();
                                _num.push(std::to_string(Cal(x2,x1,_op.top())));
                                _op.pop();

                            }
                        }
                        _op.push(s[i]);
                        numBegin = ++i;
                        break;
                default:  
                //处理转换成数字
                while(isdigit(s[i]) || s[i]=='.')
                {
                    i++;
                }
                _num.push(s.substr(numBegin, i-numBegin ));
                numBegin = i + 1;
            }
        }
        //清空运算符栈
        while(!_op.empty())
        {
            x1 = _num.top();
            _num.pop();
            x2 = _num.top();
            _num.pop();
            _num.push(std::to_string(Cal(x2,x1,_op.top())));
            _op.pop();
        }
        return stod(_num.top());
    }
    double Cal(const std::string& x, const std::string& y, char op) 
    {  
        double dx = 0.0, dy = 0.0;  
        // 将字符串转换为double  
        std::istringstream iss_x(x);  
        std::istringstream iss_y(y);  
        iss_x >> dx;  
        iss_y >> dy;  
        long long llx = stoll(x) ; 
        long long ly = stoll(y);
        switch (op) {  
            case '+':  
                return dx + dy; // 加法  
            case '-':  
                return dx - dy; // 减法  
            case '*':  
                return dx * dy; // 乘法  
            case '/':  
                // 检查除数是否为零  
                if (dy == 0) {  
                    throw ErrorMsg("Division by zero is not allowed.",Div_zero);  
                }  
                return dx / dy; // 除法  
            case '%':  
                {   
                    if (ly == 0) {  
                        throw ErrorMsg("Division by zero is not allowed.",Mod_zero);  
                    }  
                    return static_cast<double>(llx % ly); // 取模  
                }    
            case '^':  
                return std::pow(dx,dy ); // 次幂  
            default:  
                throw ErrorMsg("Division by zero is not allowed.",Unkown);  
        }  
    }
};

class Task
{
    enum 
    {
        Div_zero = 1,
        Mod_zero = 2,
        Unkown = 3
    };
    std::string _ErrorString  ="Noerorr";
    std::string _exp;
    int _ErrorCode = 0;
    double _result;
    public:
    Task(std::string&& s = "")
    :_exp(s)
    {

    }
    Task(const std:: string& s)
    :_exp(s)
    {

    }
    double Run()
    {
        try
        {
            _result = Calculate()(_exp);
        }
        catch(const ErrorMsg& e)
        {
            std::cerr << (_ErrorString  = e._MsgString)<< '\n';
            _ErrorCode = e._ErorrCode;
            return 0;
        }

        //测试答案的打印
        // std::cout<<std::left<<std::setw(7)<<_exp<<"="<<std::left<<std::setw(13)<<_result<<"  :Error:"<<_ErrorString<<"\n";
        return _result;
    }
    double operator()()
    {
        return Run();
    }

    std::string ResultString()
    {
        std::stringstream sstr;
        sstr<<std::left<<std::setw(7)<<_exp<<"="<<std::left<<std::setw(13)<<_result<<"  :Error:"<<_ErrorString;
        return sstr.str();
    }
    double Result()
    {
        return _result;
    }
    bool IsTrusted()
    {
        return _ErrorCode == Noerror;
    }
    std::string StringMsg()
    {
        return _ErrorString;
    }
};